﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ViewStock : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
          Session["PinCode"]=DALAccount.GetOneAccount(Page.User.Identity.Name).PinCode;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                string searchInformation = txtDeliverableSearch.Text.Trim();
                if (rdDeliveryNumber.Checked)
                {
                    ObjectDataSourceViewStock.SelectMethod = "SearchDeliverableOnDeliveryNumber";
                    ObjectDataSourceViewStock.SelectParameters.Clear();
                    ObjectDataSourceViewStock.SelectParameters.Add(new ControlParameter("DeliverableID", TypeCode.String, "txtDeliverableSearch", "Text"));
                    ObjectDataSourceViewStock.SelectParameters.Add(new SessionParameter("pincode", TypeCode.String, "PinCode"));
                }
                if (rdPinCode.Checked)
                {
                    ObjectDataSourceViewStock.SelectMethod = "SearchDeliverableOnPincode";
                    ObjectDataSourceViewStock.SelectParameters.Clear();
                    ObjectDataSourceViewStock.SelectParameters.Add(new ControlParameter("pincode", TypeCode.String, "txtDeliverableSearch", "Text"));
                    ObjectDataSourceViewStock.SelectParameters.Add(new SessionParameter("StockPinCode", TypeCode.String, "PinCode"));
                }
                ObjectDataSourceViewStock.DataBind();
            }
            catch
            {
                lblMessage.Text = "There was an error, the operation was terminated";
            }
        }
    }

    protected void btnAddDeliverable_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddStock.aspx");
    }
    protected void btnDeleteDeliverable_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow row in gvViewStock.Rows)
            {
               CheckBox ck= (row.FindControl("chkselect") as CheckBox);
                if (ck.Checked)
                {
                    string deliverableID = ck.ToolTip;
                    DALStock.DeleteDeliverableInStock(deliverableID, Session["PinCode"].ToString());
                }
                else continue;
            }

            Response.Redirect("ViewStock.aspx");
        }
        catch 
        {
            lblMessage.Text = "Delete Fail : There was an error, the operation was terminated";
        }
        gvViewStock.DataBind();
        
    }
}
